package sdp.group14.communication;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import sdp.group14.appdata.AppEnumData;
import sdp.group14.appdata.Configuration;
import android.util.Log;

public class ServerConnection {
	
	
	private static final int TIMEOUT_MILLISEC = 100;	
	
	public String sendRequest(String user, int actionEnum )
	{
		return null;	
	}
	
	
	public String sendRequest(String user, int action,  ArrayList<NameValuePair> nameValuePairs)
	{
        String requestPage = "hendytest.php";
        
        String url = Configuration.SERVER_IP + "/" + requestPage;   
       
        try{         	
            HttpEntityEnclosingRequestBase httpRequest = new HttpPost(url);  
            httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
            //execute the post and get the response from servers
            Log.i("serverConnection",url);
            HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
            
            if(httpResponse.getStatusLine().getStatusCode()==200){           	 
                //get the result      
                String strResult=EntityUtils.toString(httpResponse.getEntity());
                return strResult;
               }else{      
            	   Log.e("ServerConnection", "retrive result failed");
            	   return null;
               }
        }
        catch(Exception e)
        {        	
        	Log.e("ServerConnection", e.getMessage());
        	return null;
        }
        
        
	}
	

}
